package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Employee;
import cn.wolfcode.wms.domain.ProductStock;
import cn.wolfcode.wms.domain.StockIncomeBill;
import cn.wolfcode.wms.domain.StockIncomeBillItem;
import cn.wolfcode.wms.exception.LoginException;
import cn.wolfcode.wms.mapper.ProductStockMapper;
import cn.wolfcode.wms.mapper.StockIncomeBillItemMapper;
import cn.wolfcode.wms.mapper.StockIncomeBillMapper;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.StockIncomeBillQueryObject;
import cn.wolfcode.wms.service.ProductStockService;
import cn.wolfcode.wms.service.StockIncomeBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

@Service
public class StockIncomeBillServiceImpl implements StockIncomeBillService {
    @Autowired
    private StockIncomeBillMapper stockIncomeBillMapper;
    @Autowired
    private StockIncomeBillItemMapper stockIncomeBillitenMapper;
    @Autowired
    private ProductStockService productStockService;

    public void deleteById(Long id) {
        StockIncomeBill sib = stockIncomeBillMapper.selectByPrimaryKey(id);
        if (sib.getStatus()==sib.STATUS_AUDITED){
            throw new LoginException("该订单已经审核无法删除");
        }
        stockIncomeBillitenMapper.deleteByBillId(id);
        stockIncomeBillMapper.deleteByPrimaryKey(id);
    }

    public void updateOrSave(StockIncomeBill stockIncomeBill) {
        if (stockIncomeBill.getId() != null) {
           update(stockIncomeBill);
        } else {

                save(stockIncomeBill);
                   }
    }

    private void save(StockIncomeBill stockIncomeBill){

        //获取当前登录用户,设置录入相关信息
        //Employee currentUser = UserContext.getCurrentUser();
        Employee currentUser = new Employee();
        currentUser.setId(1L);
         stockIncomeBill.setInputUser(currentUser);
        stockIncomeBill.setInputTime(new Date());
        //重置订单状态为未审核h
        stockIncomeBill.setStatus(stockIncomeBill.STATUS_NORMAL);
        //计算总金额与总数量
        BigDecimal totalAmount = BigDecimal.ZERO;
        BigDecimal totalNumber = BigDecimal.ZERO;
        //遍历明细列表,用与计算金额小鸡,设置到明细对象中
        for (StockIncomeBillItem item :stockIncomeBill.getItems()){
            //计算明细金额小鸡,设置到明细对象中
            BigDecimal amount = item.getNumber().multiply(item.getCostPrice()).setScale(2, RoundingMode.HALF_UP);
            item.setAmount(amount);
            totalAmount = totalAmount.add(amount);
            totalNumber = totalNumber.add(item.getNumber());
        }
        //为订单对象设置总金额与数量
        stockIncomeBill.setTotalAmount(totalAmount);
        stockIncomeBill.setTotalNumber(totalNumber);
        //保存订单
        stockIncomeBillMapper.insert(stockIncomeBill);
        //为明细对象设置订单id,保存明细
        for (StockIncomeBillItem item :stockIncomeBill.getItems()){
            item.setBillId(stockIncomeBill.getId());
            stockIncomeBillitenMapper.insert(item);
        }
    }
    private void update(StockIncomeBill stockIncomeBill){
        StockIncomeBill sib = stockIncomeBillMapper.selectByPrimaryKey(stockIncomeBill.getId());
        if (sib.getStatus()==stockIncomeBill.STATUS_AUDITED){
            throw new LoginException("该订单已经审核无法更新");
        }
        //删除就明细
        stockIncomeBillitenMapper.deleteByBillId(stockIncomeBill.getId());
        //计算商品总金额和数量
        BigDecimal totalAmount = BigDecimal.ZERO;
        BigDecimal totalNumber = BigDecimal.ZERO;
        //遍历明细列表,用与计算金额小鸡,设置到明细对象中
        for (StockIncomeBillItem item :stockIncomeBill.getItems()){
            //为明细设置订单id
            item.setBillId(stockIncomeBill.getId());
            BigDecimal amount = item.getNumber().multiply(item.getCostPrice()).setScale(2, RoundingMode.HALF_UP);
            item.setAmount(amount);
            //更新明细
            stockIncomeBillitenMapper.insert(item);
            //计算明细金额小鸡,设置到明细对象中
            totalAmount = totalAmount.add(amount);
            totalNumber = totalNumber.add(item.getNumber());
        }
        //为订单对象设置总金额与数量
        stockIncomeBill.setTotalAmount(totalAmount);
        stockIncomeBill.setTotalNumber(totalNumber);
        //保存订单
        stockIncomeBillMapper.updateByPrimaryKey(stockIncomeBill);
        //为明细对象设置订单id,保存明细

    }



    public StockIncomeBill getById(Long id) {
        return stockIncomeBillMapper.selectByPrimaryKey(id);
    }

    public PageResult query(StockIncomeBillQueryObject qo) {
        Integer totalCount = stockIncomeBillMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult(qo.getPageSize());
        }
        List<StockIncomeBill> data = stockIncomeBillMapper.queryForList(qo);
        return new PageResult(totalCount,data,qo.getCurrentPage(),qo.getPageSize());
    }

    public void audit(Long id) {
        System.out.println(id);
        StockIncomeBill stockIncomeBill = stockIncomeBillMapper.selectByPrimaryKey(id);
        if (stockIncomeBill.getStatus()==stockIncomeBill.STATUS_NORMAL){
           /* Employee currentUser = UserContext.getCurrentUser();
            stockIncomeBill.setAuditor(currentUser);*/
            Employee employee = new Employee();
            employee.setId(1L);
            stockIncomeBill.setAuditor(employee);
            stockIncomeBill.setAuditTime(new Date());
            stockIncomeBill.setStatus(stockIncomeBill.STATUS_AUDITED);
            stockIncomeBillMapper.updatebyauditId(stockIncomeBill);

          /*  for (StockIncomeBillItem item : stockIncomeBill.getItems()) {
                ProductStock ps = productStockMapper.selectByproductIdAndDepotId(item.getProduct().getId(), stockIncomeBill.getDepot().getId());
                if (ps==null){
                    ps=new ProductStock();
                    ps.setProduct(item.getProduct());
                    ps.setDepot(stockIncomeBill.getDepot());
                    ps.setPrice(item.getCostPrice());
                    ps.setStoreNumber(item.getNumber());
                    ps.setAmount(item.getAmount());
                    productStockMapper.insert(ps);
                }else {
                    ps.setStoreNumber(ps.getStoreNumber().add(item.getNumber()));
                    ps.setAmount(ps.getAmount().add(item.getAmount()));
                    ps.setPrice(ps.getAmount().divide(ps.getStoreNumber(),2, RoundingMode.HALF_UP));
                    productStockMapper.updateByPrimaryKey(ps);
                }
            }*/
          productStockService.stockIncome(stockIncomeBill);
        }
    }
}
